package com.skhu.as.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.skhu.as.domain.ProcessStatus;
import com.skhu.as.service.ProcessStatusS;

public class ProcessStatusC extends SimpleFormController {

	private ProcessStatusS processStatusS;

	public void setProcessStatusS(ProcessStatusS processStatusS) {
		this.processStatusS = processStatusS;
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {

		if(!isFormSubmission(request)){
			if(!isFormSubmission(request)){
				boolean def = ServletRequestUtils.getBooleanParameter(request, "default", false);
				int result = 0;
				
				if(def){
					result = processStatusS.deleteAllProcessStatus();
				}
			}
		}
		
		return super.formBackingObject(request);
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		int processStatusMaxNo = processStatusS.getProcessStatusMaxNo();
		String processStatusString = ServletRequestUtils.getStringParameter(request, "processStatusString", "");
		
		processStatusS.insertProcessStatus(processStatusMaxNo + 1, processStatusString);
		
		return super.onSubmit(request, response, command, errors);
	}
	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		List<ProcessStatus> processStatusList = processStatusS.getProcessStatusList();
		
		Map ref = new HashMap();
		ref.put("processStatusList", processStatusList);
		
		return ref;
	}
	
}
